﻿using System;
using System.Collections.Generic;
using System.Linq;
using qlqa.Models.LinQ;

namespace qlqa.Delegate.ThucDonDelegate
{
    public class ThucDonDelegate
    {
        private readonly DatabaseDataContext _database = new DatabaseDataContext();
        #region Quản lý thực đơn
        /// <summary>
        /// Lấy danh sách thực đơn
        /// </summary>
        /// <returns></returns>
        public IList<tb_ThucDon> GetDanhSachThucDon(string timKiem)
        {
            var listThucDon = new List<tb_ThucDon>();
            if (string.IsNullOrEmpty(timKiem))
            {
                listThucDon = _database.tb_ThucDons.Where(t => (t.IsDelete == null || t.IsDelete == false)).ToList();
            }
            else
            {
                listThucDon = _database.tb_ThucDons.Where(t => (t.IsDelete == null || t.IsDelete == false) && (t.Ten.Contains(timKiem))).ToList();
            }
            return listThucDon;
        }
        /// <summary>
        /// Lấy thông tin thực đơn
        /// </summary>
        /// <param name="idThucDon"></param>
        /// <returns></returns>
        public tb_ThucDon GetThongTinThucDon(int idThucDon)
        {
            return _database.tb_ThucDons.FirstOrDefault(t => t.Id == idThucDon);
        }

        /// <summary>
        /// tạo mới thực đơn
        /// </summary>
        /// <param name="thucDon"></param>
        /// <returns></returns>
        public bool TaoThucDon(tb_ThucDon thucDon)
        {
            var res = false;
            try
            {
                _database.tb_ThucDons.InsertOnSubmit(thucDon);
                _database.SubmitChanges();
                res = true;
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        /// <summary>
        /// Cập nhật thực đơn
        /// </summary>
        /// <param name="thucDon"></param>
        /// <returns></returns>
        public bool CapNhatThucDon(tb_ThucDon thucDon)
        {
            var res = false;
            var thucDonOld = _database.tb_ThucDons.FirstOrDefault(t => t.Id == thucDon.Id);
            try
            {
                if (thucDonOld!= null)
                {
                    thucDonOld.Ten = thucDon.Ten;
                    thucDonOld.IsSuDung = thucDon.IsSuDung;
                    _database.SubmitChanges();
                    res = true;
                }              
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }
        /// <summary>
        /// Xóa thực đơn
        /// </summary>
        /// <param name="thucDonId"></param>
        /// <returns></returns>
        public bool XoaThucDon(int thucDonId)
        {
            var res = false;
            var thucDonOld = _database.tb_ThucDons.FirstOrDefault(t => t.Id == thucDonId);
            try
            {
                if (thucDonOld != null)
                {
                    thucDonOld.IsDelete = true;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        /// <summary>
        /// Cập nhật trạng thái sử dụng
        /// </summary>
        /// <param name="trangThai"></param>
        /// <param name="thucDonId"></param>
        /// <returns></returns>
        public bool TrangThaiSuDung(bool trangThai,int thucDonId)
        {
            var res = false;
            var thucDonOld = _database.tb_ThucDons.FirstOrDefault(t => t.Id == thucDonId);
            try
            {
                if (thucDonOld != null)
                {
                    thucDonOld.IsSuDung = trangThai;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        #endregion

        public List<tb_MonAn> TimKiemMonAn(string search)
        {
            try
            {
                if (!string.IsNullOrEmpty(search))
                {
                    search = search.Trim();
                    return (from monAn in _database.tb_MonAns
                            from thucDon in _database.tb_ThucDons
                            where monAn.IdThucDon == thucDon.Id && (monAn.IsDelete == false || monAn.IsDelete == null)
                            && monAn.IsSuDung == true && (thucDon.IsDelete == false || thucDon.IsDelete == null) && thucDon.IsSuDung == true
                            && (monAn.Ten.ToLower().Contains(search.ToLower()) || thucDon.Ten.ToLower().Contains(search.ToLower()))
                            select monAn).ToList();
                }
                return (from monAn in _database.tb_MonAns
                        from thucDon in _database.tb_ThucDons
                        where monAn.IdThucDon == thucDon.Id && (monAn.IsDelete == false || monAn.IsDelete == null)
                              && monAn.IsSuDung == true && (thucDon.IsDelete == false || thucDon.IsDelete == null) && thucDon.IsSuDung == true
                        select monAn).ToList();
            }
            catch (Exception)
            {
                return new List<tb_MonAn>();
            }
        }
    }
}